package com.guide.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.guide.bizdto.TravelTeamParam;
import com.guide.bizdto.TravelTeamSel;
import com.guide.dao.GuideApplyDao;
import com.guide.dao.GuideDao;
import com.guide.dao.TravelTeamDao;
import com.guide.domain.Account;
import com.guide.domain.Guide;
import com.guide.domain.GuideApply;
import com.guide.domain.TravelTeam;
import com.guide.enums.ErrorCodeEnum;
import com.guide.exception.ServiceException;
import com.guide.service.TravelTeamService;
import com.guide.utils.CommonUtils;
import com.guide.utils.ConstantUtils;
import com.guide.utils.SmsUtils;

@Service(value="travelTeamService")
public class TravelTeamServiceImpl implements TravelTeamService {
	private static final Logger logger = LoggerFactory
			.getLogger(TravelTeamServiceImpl.class);
	//private Date nowTime  = new Date();
	@Autowired
	private TravelTeamDao travelTeamDao;
	
	@Autowired
	private GuideDao guideDao;
	
	
	@Autowired
	private GuideApplyDao guideApplyDao;
	
	
	@Override
	public long createTravelTeam(TravelTeamParam travelTeamParam,Account account,Long travelorgId) {
		// TODO Auto-generated method stub
		TravelTeam travelTeam = param2TravelTeam(travelTeamParam,account,travelorgId);
		travelTeam.setApplycount(0);
		travelTeam.setGuidestatus(ConstantUtils.GUIDE_STATUS_0);
		travelTeam.setCommentstatus(ConstantUtils.COMMENT_STATUS_0);
		travelTeam.setKeepstatus(ConstantUtils.KEEP_STATUS_0);
		travelTeamDao.insertTravelTeam(travelTeam);
		//SMS旅行社发团，给相关导游发短信-------start--------
		try{
			List<Guide> guideList = guideDao.getGuideByTeamCity(travelTeam);
			if(!CollectionUtils.isEmpty(guideList)) {
				for(Guide guide:guideList){
					SmsUtils.sendSmsByTP(guide.getPhone(), String.format(ConstantUtils.SMS_TEMPLATE_CREATETRAVELTEAM, guide.getName(),
							String.format(ConstantUtils.SMS_TEAM_LINK, travelTeam.getId())
							));
				}
			}
		}catch(Exception e){
			logger.error("---sms----发团后发送短信出错------"+e.getMessage());
		}
		System.out.print(travelTeam.getId());
		
		//SMS旅行社发团，给相关导游发短信-------end--------
		return travelTeam.getId();
	}
	
	@Override
	public void updateTravelTeam(TravelTeamParam travelTeamParam,Account account) {
		// TODO Auto-generated method stub
		TravelTeam travelTeam = param2TravelTeam(travelTeamParam, account, null);
		travelTeam.setId(Long.valueOf(travelTeamParam.getId()));
		travelTeam.setMtime(new Date());
		travelTeamDao.updateTravelTeam(travelTeam);
	}
	
	@Override
	public void removeTravelTeam(Long id) {
		// TODO Auto-generated method stub
		TravelTeam travelTeam = travelTeamDao.getTravelTeamById(id);
		if(travelTeam.getApplycount()>0) {
			throw new ServiceException(ErrorCodeEnum.CODE_GUIDE_REMOVE_TEAM_ERROR);
		}else{
			travelTeamDao.removeTravelTeam(id);
		}
			
		
	}

	private TravelTeam param2TravelTeam(TravelTeamParam travelTeamParam,Account account,Long travelorgId){
		TravelTeam travelTeam  = new TravelTeam();
		travelTeam.setUserId(account.getUserId());
		travelTeam.setUserType(account.getUserType());
		travelTeam.setTravelorgId(travelorgId);
		travelTeam.setTeamtitle(travelTeamParam.getTeamtitle());
		travelTeam.setStartTime(CommonUtils.StrToDate(travelTeamParam.getStartTime(),"yyyy-MM-dd"));
		travelTeam.setSourcecity(travelTeamParam.getSourcecity());
		travelTeam.setTargetcity(travelTeamParam.getTargetcity());
		travelTeam.setKeepstatus(travelTeamParam.getKeepstatus());
		travelTeam.setIsoutbound(travelTeamParam.getIsoutbound());
		travelTeam.setDays(travelTeamParam.getDays());
		travelTeam.setPnum(travelTeamParam.getPnum());
		travelTeam.setTrip(CommonUtils.unescape(travelTeamParam.getTrip()));
		travelTeam.setIsSay(travelTeamParam.getIsSay());
		travelTeam.setGuidecost(travelTeamParam.getGuidecost());
		travelTeam.setDescription(travelTeamParam.getDescription());
		travelTeam.setPhoto(travelTeamParam.getPhoto());
		travelTeam.setApplycount(travelTeamParam.getApplycount());
		return travelTeam;
	}
	
	private TravelTeamParam param2TravelTeamParam(TravelTeam travelTeam){
		TravelTeamParam travelTeamParam  = new TravelTeamParam();
		travelTeamParam.setId(String.valueOf(travelTeam.getId()));
		travelTeamParam.setUserId(String.valueOf(travelTeam.getUserId()));
		travelTeamParam.setUserType(travelTeam.getUserType());
		travelTeamParam.setTravelorgId(travelTeam.getTravelorgId());
		travelTeamParam.setTeamtitle(travelTeam.getTeamtitle());
		if(travelTeam.getStartTime()!=null) {
			travelTeamParam.setStartTime(CommonUtils.DateToStr(travelTeam.getStartTime(), "yyyy-MM-dd"));
			travelTeamParam.setStartTimeCompare((travelTeam.getStartTime().before(new Date()))?ConstantUtils.STARTTIME_0:ConstantUtils.STARTTIME_1);
		}
		
		travelTeamParam.setSourcecity(travelTeam.getSourcecity());
		travelTeamParam.setTargetcity(travelTeam.getTargetcity());
		travelTeamParam.setKeepstatus(travelTeam.getKeepstatus());
		travelTeamParam.setIsoutbound(travelTeam.getIsoutbound());
		travelTeamParam.setDays(travelTeam.getDays());
		travelTeamParam.setPnum(travelTeam.getPnum());
		travelTeamParam.setTrip(travelTeam.getTrip());
		travelTeamParam.setIsSay(travelTeam.getIsSay());
		travelTeamParam.setStatus(travelTeam.getStatus());
		travelTeamParam.setGuidecost(travelTeam.getGuidecost());
		travelTeamParam.setDescription(travelTeam.getDescription());
		travelTeamParam.setPhoto(travelTeam.getPhoto());
		travelTeamParam.setApplycount(travelTeam.getApplycount());
		travelTeamParam.setGuidestatus(travelTeam.getGuidestatus());
		travelTeamParam.setCommentstatus(travelTeam.getCommentstatus());
		return travelTeamParam;
	}
	
	private List<TravelTeamParam> param2TravelTeamParamList(List<TravelTeam> travelTeamList){
		List<TravelTeamParam> retList = new ArrayList<TravelTeamParam>();
		if(!CollectionUtils.isEmpty(travelTeamList)) {
			for(TravelTeam travelTeam:travelTeamList) {
				retList.add(param2TravelTeamParam(travelTeam));
			}
		}
		return retList;
	}

	@Override
	public Map<String, Object> getTravelTeamList(Account account,TravelTeamSel travelTeamSel) {
		List<TravelTeamParam> dataList =  param2TravelTeamParamList(travelTeamDao.getTravelTeamList(account.getUserId(), travelTeamSel));
		Integer countTotal = travelTeamDao.countTravelTeamList(account.getUserId(), travelTeamSel);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put(ConstantUtils.DATE_LIST, dataList);
		retMap.put(ConstantUtils.TOTAL_COUNT, countTotal);
		return retMap;
	}
	
	@Override
	public Map<String, Object> getNewTravelTeamListByGuide(
			TravelTeamSel travelTeamSel, Account account) {
		Guide guide = guideDao.getGuideByUserId(account.getUserId());
		List<Long> teamIds = new ArrayList<Long>();
		teamIds.add(-1L);
		List<GuideApply> applys = guideApplyDao.getApplyGuidesByGuideId(guide.getId());
		if(!CollectionUtils.isEmpty(applys)) {
			for(GuideApply apply:applys) {
				teamIds.add(apply.getTeamId());
			}
		}
		List<TravelTeamParam> dataList =  param2TravelTeamParamList(travelTeamDao.getNewTravelTeamListByGuide(guide.getCity(),travelTeamSel,teamIds));
		Integer countTotal = travelTeamDao.countNewTravelTeamListByGuide(guide.getCity(), travelTeamSel,teamIds);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put(ConstantUtils.DATE_LIST, dataList);
		retMap.put(ConstantUtils.TOTAL_COUNT, countTotal);
		return retMap;
	}
	
	@Override
	public Map<String, Object> getNewTravelTeamList(TravelTeamSel travelTeamSel) {
		// TODO Auto-generated method stub
		List<TravelTeamParam> dataList =  param2TravelTeamParamList(travelTeamDao.getNewTravelTeamList(travelTeamSel));
		Integer countTotal = travelTeamDao.countNewTravelTeamList( travelTeamSel);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put(ConstantUtils.DATE_LIST, dataList);
		retMap.put(ConstantUtils.TOTAL_COUNT, countTotal);
		return retMap;
	}

	@Override
	public TravelTeamParam getTravelTeamById(Long id) {
		// TODO Auto-generated method stub
		TravelTeamParam travelTeamParam  = new TravelTeamParam();
		TravelTeam travelTeam  = travelTeamDao.getTravelTeamById(id);
		if(travelTeam!=null) {
			travelTeamParam = param2TravelTeamParam(travelTeam);
		}
		return travelTeamParam;
	}

	@Override
	public Map<String, Object> getTravelTeamByStatus(Account account,
			TravelTeamSel travelTeamSel) {
		List<TravelTeamParam> dataList =  param2TravelTeamParamList(travelTeamDao.getTravelTeamListByStatus(account.getUserId(), travelTeamSel));
		Integer countTotal = travelTeamDao.countTravelTeamListByStatus(account.getUserId(),travelTeamSel);
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put(ConstantUtils.DATE_LIST, dataList);
		retMap.put(ConstantUtils.TOTAL_COUNT, countTotal);
		return retMap;
	}



	

	

	




}
